package com.seeqool.v1;

import java.sql.SQLException;
import java.util.Vector;

public class QueryManager {
	private Vector<QueryThread> dbThreads = new Vector<QueryThread>();
	private String dbURL;
	private String dbUser;
	private String dbPass;
	
	public QueryManager(String url, String username, String password) throws SQLException {
		super();
		dbURL = url;
		dbUser = username;
		dbPass = password;
	}
	
	public synchronized int query(String SQL, QueryCallback queryCallback, int queryID) throws SQLException {
		int t = 0;
		while ((t<dbThreads.size()) && (!dbThreads.get(t).execute(SQL, queryCallback, queryID))) t+=1;
		if (t==dbThreads.size()) dbThreads.add(new QueryThread(dbURL, dbUser, dbPass, SQL, queryCallback, queryID));
		return t;
	}
	
	public boolean cancel(int query) {
		try {
			dbThreads.get(query).cancel();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
